#!/bin/bash

source env.sh
source parameters.sh
source functions.sh

mylog=gen.init.log
rm -f $mylog
cwd=`pwd`
cp -a $init_tool_dir/bond.table .
rm -fr tools
mkdir -p tools
mv bond.table ./tools
init_tool_dir=$cwd/tools
make -C $init_tool_dir/bond.table clean &> /dev/null
make -j -C $init_tool_dir/bond.table &> /dev/null

for length in `seq $init_interval_start $init_center_length_interval $init_interval_end`
do
    pa=`printf %.2f $length`
    echo "# doing length $pa" 
    workdir=./init.conf.$pa
    if test -d $workdir; then
	mv $workdir $workdir.`date +%s`
    fi
    mkdir $workdir
    cd $workdir    
    
    cp $init_gmx_template/conf.gro .
    cp $init_gmx_template/grompp.mdp .
    cp $init_gmx_template/topol.top .
    cp $init_gmx_template/index.ndx .
    
    echo "# gen constrain with command:     $init_tool_dir/bond.table/constraint -k $init_constraint_k -p $length -w $init_center_length_interval --x0 $init_table_start --x1 $init_table_end &>> $mylog"
    $init_tool_dir/bond.table/constraint -k $init_constraint_k -p $length -w $init_center_length_interval --x0 $init_table_start --x1 $init_table_end &>> $mylog
    
    set_parameters_init_warm grompp.mdp
    grompp -n index.ndx &>> $mylog
    mdrun &>> $mylog

    mv -f confout.gro conf.gro
    grocleanit
    
    set_parameters_init grompp.mdp
    grompp -n index.ndx &>> $mylog
    mdrun &>> $mylog
    
    cd $cwd
done
